package com.sfzlot.dev.netty.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.sfzlot.dev.netty.constant.Constant.*;

@Api(tags = "HTTP调用平台开关接口")
@RestController
@CrossOrigin("*")
@RequestMapping(value = "/api/rest")
public class HttpRestController {
    final static Logger logger = LoggerFactory.getLogger(HttpRestController.class);
    @Value("${sfzlot.http.url}")
    private String url;
    @Value("${sfzlot.http.version}")
    private String version;
    @Value("${sfzlot.http.signType}")
    private String signType;
    @Value("${sfzlot.http.account}")
    private String account;
    @Value("${sfzlot.http.sercet}")
    private String sercet;

    /**
     * 设备登出
     *
     * @param vin
     */
    @ApiOperation(value = "设备登出")
    @PostMapping(value = "/logout")
    public String logout(@RequestParam(required = true)String vin) {
        Map<String, String> map = initMap();
        map.put("sign", getSign(HTTP_LOGOUT, map, sercet));

        return HttpRequest.post(url + HTTP_LOGOUT)
                .addHeaders(map)
                .form("vin", vin)
                .timeout(20000)//超时，毫秒
                .execute().body();
    }

    /**
     * 更改服务器
     *
     * @param vin
     * @param serverUrl
     */
    @ApiOperation(value = "更改服务器")
    @PostMapping(value = "/updateServer")
    public String updateServer(@RequestParam(required = true)String vin, @RequestParam(required = true)String serverUrl) {
        Map<String, String> map = initMap();
        map.put("sign", getSign(HTTP_UPDATE_SERVER, map, sercet));

        return HttpRequest.post(url + HTTP_UPDATE_SERVER)
                .addHeaders(map)
                .form("vin", vin)
                .form("url", serverUrl)
                .timeout(20000)//超时，毫秒
                .execute().body();
    }

    /**
     * 更改初使值
     *
     * @param vin
     * @param ssid
     * @param password
     */
    @ApiOperation(value = "更改初使值")
    @PostMapping(value = "/updateSettingIn")
    public String updateSettingIn(@RequestParam(required = true)String vin, @RequestParam(required = true)String ssid, @RequestParam(required = true)String password) {
        Map<String, String> map = initMap();
        map.put("sign", getSign(HTTP_UPDATE_SETTING, map, sercet));

        return HttpRequest.post(url + HTTP_UPDATE_SETTING)
                .addHeaders(map)
                .form("vin", vin)
                .form("ssid", ssid)
                .form("password", password)
                .timeout(20000)//超时，毫秒
                .execute().body();
    }

    /**
     * 获取终端信息
     *
     * @param vin
     */
    @ApiOperation(value = "获取终端信息")
    @PostMapping(value = "/info")
    public String info(@RequestParam(required = true)String vin) {
        Map<String, String> map = initMap();
        map.put("sign", getSign(HTTP_INFO, map, sercet));

        return HttpRequest.post(url + HTTP_INFO)
                .addHeaders(map)
                .form("vin", vin)
                .timeout(20000)//超时，毫秒
                .execute().body();
    }

    /**
     * 通电
     *
     * @param vin
     * @param times
     */
    @ApiOperation(value = "通电")
    @PostMapping(value = "/start")
    public String start(@RequestParam(required = true)String vin, @RequestParam(required = true)String times) {
        Map<String, String> map = initMap();
        map.put("sign", getSign(HTTP_START, map, sercet));

        return HttpRequest.post(url + HTTP_START)
                .addHeaders(map)
                .form("vin", vin)
                .form("times", times)
                .timeout(20000)//超时，毫秒
                .execute().body();
    }

    /**
     * 断电
     */
    @ApiOperation(value = "断电")
    @PostMapping(value = "/stop")
    public String stop(@RequestParam(required = true)String vin, @RequestParam(required = true)String times) {
        Map<String, String> map = initMap();
        map.put("sign", getSign(HTTP_STOP, map, sercet));

        return HttpRequest.post(url + HTTP_STOP)
                .addHeaders(map)
                .form("vin", vin)
                .form("times", times)
                .timeout(20000)//超时，毫秒
                .execute().body();
    }


    /**
     * 重置设备
     *
     * @param vin
     */
    @ApiOperation(value = "重置设备")
    @PostMapping(value = "/reset")
    public String reset(@RequestParam(required = true)String vin) {
        Map<String, String> map = initMap();
        map.put("sign", getSign(HTTP_RESET, map, sercet));

        return HttpRequest.post(url + HTTP_RESET)
                .addHeaders(map)
                .form("vin", vin)
                .timeout(20000)//超时，毫秒
                .execute().body();
    }

    /**
     * 物理开关是否有效
     *
     * @param vin
     * @param flag
     */
    @ApiOperation(value = "物理开关是否有效")
    @PostMapping(value = "/isSwitch")
    public String isSwitch(@RequestParam(required = true)String vin, @RequestParam(required = true)String flag) {
        Map<String, String> map = initMap();
        map.put("sign", getSign(HTTP_ISSWITCH, map, sercet));

        return HttpRequest.post(url + HTTP_ISSWITCH)
                .addHeaders(map)
                .form("vin", vin)
                .form("flag", flag)
                .timeout(20000)//超时，毫秒
                .execute().body();
    }

    /**
     * 初使化参数
     * @return map
     */
    private Map<String, String> initMap() {
        Map<String, String> map = Maps.newHashMap();
        map.put("account", account);
        map.put("version", version);
        map.put("timestamp", System.currentTimeMillis() + "");
        map.put("signType", signType);
        return map;
    }

    /**
     * 根据参数获取签名
     * @param url 路径
     * @param map 参数合集
     * @return
     */
    private String getSign(String url, Map<String, String> map, String sercet) {
        MapUtil.sort(map);
        StringBuilder toSign = new StringBuilder(url)
                .append("&")
                .append(HttpUtil.toParams(map))
                .append("&")
                .append(sercet);
//        logger.info("签名前的参数encode之后:{}",URLUtil.encode(toSign.toString()));
        return SecureUtil.md5(URLUtil.encode(toSign.toString()));
    }
}
